package cn.yunhe.control;


import cn.yunhe.entity.User;
import cn.yunhe.server.UserServer;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/ManageUserServlet")
public class ManageUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int uId = Integer.parseInt(request.getParameter("uId")); //获取用户编号
        int value = Integer.parseInt(request.getParameter("value")); //获取用户修改操作值

        UserServer userServer = new UserServer();

        User user =  userServer.getUserById(uId);  //通过用户Id获取用户全部信息并赋予一个用户对象
        Map<String,Object> map = new HashMap<String, Object>();  //创建map对象
        map.put("user",user);  //向map对象添加属性
        map.put("flag",value);
        int flag = userServer.changeUserState(map);//将用户禁言,传入用户操作标记值标记
        if(flag>0){  //返回值大于0说明修改状态成功,小于等于0说明状态修改失败
            request.setAttribute("result","状态修改成功");
            request.getRequestDispatcher("userManager.jsp").forward(request,response); //跳转到原页面
        }else {
            request.setAttribute("result","状态修改失败");
            request.getRequestDispatcher("userManager.jsp").forward(request,response); //跳转到原页面
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
